package com.sky.common.task;

import com.sky.business.sys.domain.Group;
import com.sky.business.sys.queue.MessageConsumerRunnable;
import com.sky.business.sys.service.GroupService;
import com.sky.business.sys.service.MQTTMessageService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化任务
 */
@Component
@Slf4j
public class InitTask {

    @Autowired
    private MQTTMessageService mqttMessageService;

    @Autowired
    private GroupService groupService;

    @PostConstruct
    public void init(){
        //启动一条线程 用于读取队列中的MQTT消息保存到数据库
        new Thread(new MessageConsumerRunnable(mqttMessageService)).start();
        //创建默认分组
        List<Group> groupList = groupService.list();
        if(groupList.isEmpty()){
            Group group = new Group();
            group.setName("默认");
            group.setDescription("默认");
            groupService.save(group);
        }
    }
}
